package com.cheyunkeji.er.view.evaluate.wheel_selector;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cheyun.netsalev3.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 15-4-20.
 */
public class ListPicker<T extends InfoData> {

    String tag = "";
    ArrayList<T> listList = new ArrayList<>();
    View v;
    View view = null;
    Context context;
    IListPickerOK ilistPickerOK;
    LinearLayout llBack;
    LinearLayout llFront;
    TextView tv_city_yes;
    TextView tv_city_no;
    private LayoutInflater inflater = null;
    private WheelView list;
    private PopupWindow popupWindow;

    public ListPicker(Context context, IListPickerOK ilistPickerOK, View v) {
        this.v = v;
        this.context = context;
        this.ilistPickerOK = ilistPickerOK;
    }

    public static void hidePop(PopupWindow mPop) {
        if (mPop != null && mPop.isShowing()) {
            mPop.dismiss();
        }
    }

    public void setIlistPickerOK(IListPickerOK ilistPickerOK) {
        this.ilistPickerOK = ilistPickerOK;
    }

    public void setList(String tag, ArrayList<T> listList) {
        this.tag = tag;
        this.listList.clear();
        this.listList.addAll(listList);
    }

    private View getListPick() {
        view = inflater.inflate(R.layout.er_wheel_list_picker, null);
        view.measure(View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED);
        initView();

        ArrayListWheelAdapter<T> arrayListWheelAdapter1 = new ArrayListWheelAdapter(context, listList);
        list.setViewAdapter(arrayListWheelAdapter1);

        list.setVisibleItems(7);

        //year.setCyclic(true);
        //year.setCurrentItem(time.get(Calendar.YEAR) - 1950);

        return view;
    }

    private void initView() {
        list = (WheelView) view.findViewById(R.id.list);
        llBack = (LinearLayout) view.findViewById(R.id.llBack);
        llFront = (LinearLayout) view.findViewById(R.id.llFront);
        tv_city_no = (TextView) view.findViewById(R.id.tv_date_no);
        tv_city_yes = (TextView) view.findViewById(R.id.tv_date_yes);
        tv_city_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hidePop(popupWindow);
            }
        });
        tv_city_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listList.size() > 0) {
                    ilistPickerOK.onListOK(tag, listList.get(list.getCurrentItem()));
                }
                hidePop(popupWindow);
            }
        });
        llBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        hidePop(popupWindow);
                        return true;
                    }
                    default:
                        break;
                }
                return false;
            }
        });

        llFront.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    public void listPickerDialog() {
        if (popupWindow != null) {
            hidePop(popupWindow);
        }
        inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        popupWindow = new PopupWindow(getListPick());
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        popupWindow.setTouchable(true);
        //popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        //popupWindow.setOutsideTouchable(true);
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
    }


    public boolean isListPickerShowing() {

        if(popupWindow != null)
        return popupWindow.isShowing();

        return false ;
    }


    public PopupWindow getPopupWindow() {
        if (popupWindow != null) {
            return popupWindow;
        }
        return null;
    }

}
